/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.mgt.server.service.power.impl;

import com.iwindplus.mgt.domain.constant.MgtConstant.RedisCacheConstant;
import com.iwindplus.mgt.domain.dto.power.UserDepartmentDTO;
import com.iwindplus.mgt.server.dal.repository.power.UserDepartmentRepository;
import com.iwindplus.mgt.server.service.power.UserDepartmentService;
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户部门关系业务层接口实现类.
 *
 * @author zengdegui
 * @since 2018/9/1
 */
@Service
@CacheConfig(cacheNames = {RedisCacheConstant.CACHE_USER_DEPARTMENT})
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class UserDepartmentServiceImpl implements UserDepartmentService {

    private final UserDepartmentRepository userDepartmentRepository;

    @Caching(
        evict = {
            @CacheEvict(cacheNames = {RedisCacheConstant.CACHE_USER_DEPARTMENT}, allEntries = true),
            @CacheEvict(cacheNames = {RedisCacheConstant.CACHE_DEPARTMENT}, allEntries = true)
        }
    )
    @Override
    public boolean save(UserDepartmentDTO entity) {
        return this.userDepartmentRepository.save(entity);
    }

    @Caching(
        evict = {
            @CacheEvict(cacheNames = {RedisCacheConstant.CACHE_USER_DEPARTMENT}, allEntries = true),
            @CacheEvict(cacheNames = {RedisCacheConstant.CACHE_DEPARTMENT}, allEntries = true)
        }
    )
    @Override
    public boolean saveBatchUser(Long departmentId, Set<Long> userIds) {
        return this.userDepartmentRepository.saveBatchUser(departmentId, userIds);
    }

    @Caching(
        evict = {
            @CacheEvict(cacheNames = {RedisCacheConstant.CACHE_USER_DEPARTMENT}, allEntries = true),
            @CacheEvict(cacheNames = {RedisCacheConstant.CACHE_DEPARTMENT}, allEntries = true)
        }
    )
    @Override
    public boolean saveBatchDepartment(Long userId, Set<Long> departmentIds) {
        return this.userDepartmentRepository.saveBatchDepartment(userId, departmentIds);
    }

    @Caching(
        evict = {
            @CacheEvict(cacheNames = {RedisCacheConstant.CACHE_USER_DEPARTMENT}, allEntries = true),
            @CacheEvict(cacheNames = {RedisCacheConstant.CACHE_DEPARTMENT}, allEntries = true)
        }
    )
    @Override
    public boolean saveBatch(List<UserDepartmentDTO> entities) {
        return this.userDepartmentRepository.saveBatch(entities);
    }

    @Caching(
        evict = {
            @CacheEvict(cacheNames = {RedisCacheConstant.CACHE_USER_DEPARTMENT}, allEntries = true),
            @CacheEvict(cacheNames = {RedisCacheConstant.CACHE_DEPARTMENT}, allEntries = true)
        }
    )
    @Override
    public boolean editBatchUser(Long departmentId, Set<Long> userIds) {
        return this.userDepartmentRepository.editBatchUser(departmentId, userIds);
    }

    @Caching(
        evict = {
            @CacheEvict(cacheNames = {RedisCacheConstant.CACHE_USER_DEPARTMENT}, allEntries = true),
            @CacheEvict(cacheNames = {RedisCacheConstant.CACHE_DEPARTMENT}, allEntries = true)
        }
    )
    @Override
    public boolean editBatchDepartment(Long userId, Set<Long> departmentIds) {
        return this.userDepartmentRepository.editBatchDepartment(userId, departmentIds);
    }

}
